﻿namespace Infra.Validation
{
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// Class which represents a set of rules to validate a specific entity.
    /// </summary>
    public abstract class RulesSet<T> : IEnumerable<ValidationRuleFor<T>> where T : class
    {
        private RulesList<ValidationRuleFor<T>> RulesList { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="RulesSet&lt;T&gt;"/> class.
        /// </summary>
        protected RulesSet()
        {
            this.RulesList = new RulesList<ValidationRuleFor<T>>();   
        }

        /// <summary>
        /// Adds the rule to the rule set.
        /// </summary>
        /// <param name="rule">The rule.</param>
        protected void AddRule(ValidationRuleFor<T> rule)
        {
            this.RulesList.Add(rule.RuleNumber, rule);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<ValidationRuleFor<T>> GetEnumerator()
        {
            return this.RulesList.Values.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
